import React, { useEffect, useRef } from "react"

function useDebounce(callback, delay) {
  const timer = useRef(null)
  useEffect(() => {
    return () => {
      if (timer.current) clearTimeout(timer.current)
    }
  }, [])
  return (...arg) => {
    if (timer.current) clearTimeout(timer.current)
    timer.current = setTimeout(() => {
      callback(...arg)
    }, delay)
  }
}

export default useDebounce
